package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import com.sky.utils.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Resource
    private AddressBookMapper addressBookMapper;

    /**
     * @description: 新增地址
     * @author: tanbobin
     * @date: 2024/6/6 11:21
     * @param:
     * @param addressBook
     * @return:
     **/
    @Override
    public void add(AddressBook addressBook) {
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBookMapper.add(addressBook);
    }

    /**
     * @description: 查询当前用户所有地址列表
     * @author: tanbobin
     * @date: 2024/6/6 11:32
     * @param:
     * @return:
     * @return java.util.List<com.sky.entity.AddressBook>
     **/
    @Override
    public List<AddressBook> queryList() {
        return addressBookMapper.queryList();
    }

    /**
     * @description: 设置默认地址
     * @author: tanbobin
     * @date: 2024/6/6 11:38
     * @param:
     * @param addressBook
     * @return:
     **/
    @Override
    public void setDefaultAddress(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        //1.1 把修改为非默认地址
        addressBook.setIsDefault(0);
        // 1.2 获取用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateDefaultAddress(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        addressBookMapper.setDefaultAddress(addressBook);
    }

    /**
     * @description: 查询默认地址
     * @author: tanbobin
     * @date: 2024/6/6 12:44
     * @param:
     * @return: 
     * @return com.sky.entity.AddressBook
     **/
    @Override
    public AddressBook queryDefault() {
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder().isDefault(1).userId(userId).build();

        return addressBookMapper.queryAddress(addressBook);
    }

    /**
     * @description: 根据id查询地址
     * @author: tanbobin
     * @date: 2024/6/6 13:01
     * @param:
     * @param id
     * @return:
     * @return com.sky.entity.AddressBook
     **/
    @Override
    public AddressBook queryAddressById(Long id) {
        return addressBookMapper.queryAddressById(id);
    }

    /**
     * @description: 根据id修改地址
     * @author: tanbobin
     * @date: 2024/6/6 13:06
     * @param:
     * @param addressBook
     * @return:
     **/
    @Override
    public void updateAddress(AddressBook addressBook) {
        addressBookMapper.updateAddress(addressBook);
    }

    /**
     * @description: 根据id删除地址
     * @author: tanbobin
     * @date: 2024/6/6 13:24
     * @param:
     * @param id
     * @return:
     **/
    @Override
    public void deleteAddress(Long id) {
        addressBookMapper.deleteAddress(id);
    }
}
